const VueSSRServerPlugin = require('vue-server-renderer/server-plugin');
const VueSSRClientPlugin = require('vue-server-renderer/client-plugin');
const nodeExternals = require('webpack-node-externals');
const merge = require('lodash.merge');
const CopyPlugin = require('copy-webpack-plugin');

const path = require('path');

const resolve = file => path.resolve(__dirname, file);
const TARGET_NODE = process.env.BUILD_TARGET === 'node';

const createApiFile = TARGET_NODE
  ? './create-api-server.js'
  : './create-api-client.js';

const target = TARGET_NODE
  ? 'server'
  : 'client';

module.exports = {
  lintOnSave: false,
  // baseUrl: process.env.NODE_ENV === "production" ? '/' : '/',
  publicPath: process.env.NODE_ENV === 'production' ? '/' : '/',
  productionSourceMap: undefined,
  outputDir: 'dist/client',
  indexPath: './dist/index.html',
  devServer: {
    index: 'index.html',
    disableHostCheck: true,
    open: process.platform === 'darwin',
    https: false,
    hotOnly: false,
    proxy: {
      '/api': {
        target: 'http://13.251.6.196:10800/api',
        changeOrigin: true,
        pathRewrite: {
          '^/api': '',
        },
      },
      '/client_token': {
        target: 'http://127.0.0.1:8856',
        changeOrigin: true,
      },
      '/checkout': {
        target: 'http://127.0.0.1:8856',
        changeOrigin: true,
      },
    },
    headers: {
      'Access-Control-Allow-Origin': '*',
    },
  },
  css: {
    loaderOptions: {
      postcss: {
        plugins: [],
      },
    },
    extract: process.env.NODE_ENV === 'production',
    sourceMap: process.env.NODE_ENV === 'production',
  },
  assetsDir: 'assets',
  runtimeCompiler: true,
  parallel: undefined,
  configureWebpack: () => ({
    entry: `./src/entry-${target}`,
    target: TARGET_NODE ? 'node' : 'web',
    node: TARGET_NODE ? undefined : false,
    plugins: [
      TARGET_NODE
        ? new VueSSRServerPlugin()
        : new VueSSRClientPlugin(),
      new CopyPlugin([

        {
          from: resolve('./package.json'),
          to: resolve('./dist'),
        },

        {
          from: resolve('./server'),
          to: resolve('./dist/server'),
          toType: 'dir',
          ignore: [
            'setup-dev-server.js',
            'pm2.config.template.js',
            '.DS_Store',
          ],
        },
        {
          from: resolve('./ecosystem.config.js'),
          to: resolve('./dist/ecosystem.config.js'),
          transform(content) {
            return content
              .toString()
              .replace('NODE_ENV_VALUE', process.env.NODE_ENV)
              .replace('NODE_PORT_VALUE', process.env.NODE_PORT)
              .replace('NODE_DEPLOY_VALUE', process.env.NODE_DEPLOY);
          }
        }
      ])
    ],
    externals: TARGET_NODE ? nodeExternals({
      whitelist: [/\.css$/, /\?vue&type=style/],
    })
      : undefined,
    optimization: {
      splitChunks: TARGET_NODE ? false : undefined,
    },
    output: {
      libraryTarget: TARGET_NODE
        ? 'commonjs2' : undefined,
    },
    resolve: {
      alias: {
        'create-api': createApiFile,
        '@': path.resolve(__dirname, './src'),
        '@assets': path.resolve(__dirname, './src/assets'),
      },
    },
  }),
  chainWebpack: (config) => {
    // config.resolve.alias
    // .set("@", resolve("src"))
    // .set('create-api': createApiFile,)
    // .set("@assets", resolve("src/assets"));

    config.module
      .rule('vue')
      .use('vue-loader')
      .tap(options => merge(options, {
        optimizeSSR: false,
      }));

    // if (TARGET_NODE) {
    // config.plugins.delete("hmr");
    // }
  },

};
